package com.yyjj.yyjj.acl.controller;

import com.yyjj.yyjj.acl.service.PermissionService;
import com.yyjj.yyjj.common.result.Result;
import com.yyjj.yyjj.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/permission")
@Api(tags = "菜单管理")
@RequiredArgsConstructor
public class PermissionController {

    private final PermissionService permissionService;

    //获取树型菜单
    @ApiOperation(value = "获取菜单")
    @GetMapping
    public Result list() {
        List<Permission> permissionList = permissionService.queryAllPermission();
        return Result.ok(permissionList);
    }

    //新增菜单
    @ApiOperation(value = "新增菜单")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission){
        permissionService.save(permission);
        return Result.ok(null);
    }

    //修改菜单
    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return Result.ok(null);
    }

    //删除菜单
    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable("id") Long id){
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    @ApiOperation(value = "根据角色获取菜单数据")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        Map<String, Object> roleMap = permissionService.findPermissionByRoleId(roleId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long roleId,@RequestParam Long[] permissionId) {
        permissionService.saveRolePermissionRealtionShip(roleId,permissionId);
        return Result.ok(null);
    }

}
